<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="房间类型" prop="roomTypeName">
        <el-select v-model="queryParams.roomTypeName" placeholder="请选择房间类型" clearable>
          <el-option
            v-for="dict in dict.type.user_room_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hotel:hlroomdevice:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hotel:hlroomdevice:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hotel:hlroomdevice:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hotel:hlroomdevice:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hlroomdeviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="房间类型" align="center" prop="roomType.name" />

      <el-table-column label="设备详情" align="center">
        <template slot-scope="scope">
          <el-button type="info" plain size="small" @click="catDevice(scope.row)">查看设备</el-button>
        </template>
      </el-table-column>

      <el-table-column label="备注" align="center" prop="remark" />

      <el-table-column label="修改者" align="center" prop="updateBy" />
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hotel:hlroomdevice:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hotel:hlroomdevice:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

<!--    查看设备详细信息按钮弹出对话框-->
    <el-dialog
      :title="tempTitle"
      :visible.sync="catDevices"
      width="570px"
      center>

      <table style="background-color: whitesmoke">
        <tr style="text-align: center">
          <td style="background-color: #888a8e;width: 340px;color: white">设备名称</td>
          <td style="background-color: #888a8e;width: 180px;color: white">设备数量</td>
        </tr>
      </table>

      <div v-for="(item, index) of deviceCountList">
        <div v-if="index % 2 === 0">
          <table>
            <tr style="text-align: center">
              <td style="width: 340px;border: 1px solid gainsboro">{{item.name}}</td>
              <td style="width: 180px;border: 1px solid gainsboro">{{item.count}}</td>
            </tr>
          </table>
        </div>
        <div v-else>
          <table>
            <tr style="text-align: center">
              <td style="width: 340px;border: 1px solid gainsboro;background-color: whitesmoke">{{item.name}}</td>
              <td style="width: 180px;border: 1px solid gainsboro;background-color: whitesmoke">{{item.count}}</td>
            </tr>
          </table>
        </div>
      </div>

      <span slot="footer" class="dialog-footer" style="margin-left: 450px">
        <el-button type="info" @click="catDevices = false" size="small">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加房间设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="deviceForm" :rules="rules" label-width="80px">

        <el-form-item label="房间类型" :prop="deviceForm.roomTypeName">
          <div v-if="updateKey === true">
            <el-input v-model="deviceForm.roomTypeName" style="width: 220px" disabled size="small" :value="deviceForm.roomTypeName"></el-input>
          </div>
          <div v-else>
            <el-select v-model="deviceForm.roomTypeName" placeholder="请选择房间类型" clearable size="small">
              <el-option
                v-for="dict in dict.type.user_room_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </div>
        </el-form-item>

        <table style="text-align: center;background-color: whitesmoke;width: 96%;margin: 0 auto;margin-bottom: 10px">
          <tr>
            <td style="padding-left: 55px">设备名</td>
            <td style="padding-right: 45px">数量</td>
          </tr>
        </table>
        <div v-if="updateKey === true">
          <el-form-item
            :label="'新设备' + (index+1) + ':'"
            v-for="(item, index) in deviceForm.roomDeviceJson">

              <el-select v-model="item.name" :value="item.name"  :prop="'roomDeviceJson.' + index + '.name'" placeholder="选择设备" clearable size="small" style="width: 180px">
                <el-option
                  v-for="temp in Devices"
                  :key="temp.deviceName"
                  :label="temp.deviceName"
                  :value="temp.deviceName"
                />
              </el-select>
              <span style="padding-left: 35px" />
              <el-select v-model="item.count" :value="item.count" :prop="'roomDeviceJson.' + index + '.count'" placeholder="选择数量" clearable size="small" style="width: 110px">
                <el-option
                  v-for="dict in 20"
                  :key="dict"
                  :label="dict"
                  :value="dict"
                />
              </el-select>
              <span style="padding-left: 20px" />
              <el-link  @click.prevent="removeDomain(item)" type="danger" :underline="false">删除</el-link>

          </el-form-item>
        </div>
        <div v-else>
          <el-form-item
            :label="'新设备' + (index+1) + ':'"
            v-for="(item, index) in deviceForm.roomDeviceJson">

              <el-select v-model="item.name" :prop="'roomDeviceJson.' + index + '.name'" placeholder="选择设备" clearable size="small" style="width: 180px">
                <el-option
                  v-for="temp in Devices"
                  :key="temp.deviceName"
                  :label="temp.deviceName"
                  :value="temp.deviceName"
                />
              </el-select>
              <span style="padding-left: 35px" />
              <el-select v-model="item.count" :prop="'roomDeviceJson.' + index + '.count'" placeholder="选择数量" clearable size="small" style="width: 110px">
                <el-option
                  v-for="dict in 20"
                  :key="dict"
                  :label="dict"
                  :value="dict"
                />
              </el-select>
              <span style="padding-left: 20px" />
              <el-link  @click.prevent="removeDomain(item)" type="danger" :underline="false">删除</el-link>

          </el-form-item>
        </div>
        <el-form-item>
          <el-link type="primary" @click="addDomain" :underline="false">新增域名</el-link>
        </el-form-item>


      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" plain size="small">确 定</el-button>
        <el-button @click="cancel" size="small">取 消</el-button>
      </div>
    </el-dialog>



  </div>
</template>

<script>
import { listHlroomdevice, getHlroomdevice, delHlroomdevice, updateRoomDevice, DataHlroomdeviceByRoomTypeName,addHlroomTypeAndJson} from "@/api/hotel/hlroomdevice";
import { getDevices } from "@/api/hotel/hldevice";

export default {
  name: "Hlroomdevice",
  dicts: ['user_room_type'],
  data() {
    return {

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 临时title
      tempTitle:"",
      // 总条数
      total: 0,
      // 修改摊开的v-for标识
      updateKey: false,
      // 总设备
      Devices: {},

      // 酒店房间设备表格数据
      hlroomdeviceList: [],
      // 酒店房间设备查询数据
      deviceCountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查看设备开关
      catDevices:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roomTypeName: null,
      },
      // 表单参数
      form: {},
      // 设备信息传输
      deviceForm:{
        roomTypeName: '',
        roomDeviceJson: [{
          name:'',
          count:'',
        }],
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    // 打开查看设备
    catDevice(row){
      // 清空数据
      this.tempTitle = ""
      this.deviceCountList = [];
      // 将值赋值给该变量
      this.tempTitle = row.roomType.name+"设备数量详细信息"
      this.deviceCountList = JSON.parse(row.deviceNameAndCount);
      this.catDevices = true;
    },


    // 添加新增设备弹出框的附加
    addDomain() {
      this.deviceForm.roomDeviceJson.push({
        name: '',
        count:'',
        key: Date.now()
      })
      },


    // 删除新增设备弹出框的附加
    removeDomain(item) {
      var index = this.deviceForm.roomDeviceJson.indexOf(item)
      if (index !== -1) {
        this.deviceForm.roomDeviceJson.splice(index, 1)
      }
    },



// 新增按钮提交
    addRoomDevice(){
      this.reset()
      this.addRoomDeviceData()
    },

    // 新增提交方法
    addRoomDeviceData(){
      console.log(this.deviceForm.roomDeviceJson)
      addHlroomTypeAndJson(this.deviceForm).then(response => {

        this.$modal.msgSuccess(response.msg);
        this.resetDeviceForm()
        this.open = false
        this.getList()
      }
      );
    },


    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {

            console.log(this.deviceForm)
            updateRoomDevice(this.deviceForm).then(response =>{

              this.$modal.msgSuccess(response.msg);
              this.open = false;
              this.resetDeviceForm()
              this.deviceForm.roomTypeName = ''
              this.deviceForm.roomDeviceJson = []
              this.updateKey = false;
              this.getList();

            })
          } else {
            this.addRoomDevice()
          }
        }
      });
    },


    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.selectAllDevice();
      this.deviceForm.roomTypeName = ''
      this.deviceForm.roomDeviceJson = []
      // 这里可以查询也可以不查询留着id查的方法js，看以后是否可以用得上
      // const id = row.id || this.ids
      // getHlroomdevice(id).then(response => {
        this.form = row;
        this.deviceForm.roomTypeName = row.roomType.name;
        this.deviceCountList = [];
        this.deviceForm.roomDeviceJson = JSON.parse(row.deviceNameAndCount);

        this.updateKey = true;
        this.open = true;
        this.title = "修改房间设备";
      // })
    },


    /** 查询酒店房间设备列表 */
    getList() {
      this.loading = true;
      listHlroomdevice(this.queryParams).then(response => {
        this.hlroomdeviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.updateKey = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        roomTypeId: null,
        deviceNameAndCount: null,
        state: null,
        roomType: null,
        remark: null,
        createBy: null,
        updateBy: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },

    // 表单重置
    resetDeviceForm() {
      this.deviceForm={
        roomTypeName: '',
          roomDeviceJson: [{
          name:'',
          count:'',
        }],
      };
      this.resetForm("deviceForm");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if(this.queryParams.roomTypeName == null || this.queryParams.roomTypeName == ''){
        this.getList()
      }else {
        this.loading = true;
        DataHlroomdeviceByRoomTypeName(this.queryParams.roomTypeName).then(response => {
          this.$modal.msgSuccess(response.msg);
          this.hlroomdeviceList = response.data;
          this.loading = false;
        });
      }
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.selectAllDevice();
      this.resetDeviceForm();
      this.open = true;
      this.title = "添加房间设备";
    },

    /** 查询所有设备操作 */
    selectAllDevice() {
      getDevices().then(response => {
        this.Devices = response;
      });
    },


    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除酒店房间设备编号为"' + ids + '"的数据项？').then(function() {
        return delHlroomdevice(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('hotel/hlroomdevice/export', {
        ...this.queryParams
      }, `hlroomdevice_${new Date().getTime()}.xlsx`)
    }




  }
};
</script>
